<template>
    <Teleport to="body">
        <!-- <Transition name="loading-fade"> -->
        <div v-if="isPageLoading" class="app-loading">
            <div class="loading-container">
                <div class="loading-spinner"></div>
                <div class="loading-text">{{ apptitle }}</div>
                <div class="loading-subtitle">{{ loadingText }}<span class="loading-dots"></span></div>
                <div class="loading-progress"></div>
            </div>
        </div>
        <!-- </Transition> -->
    </Teleport>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { useLoadingStore } from '@/stores/modules/loading'

const loadingStore = useLoadingStore()
const { isPageLoading, loadingText } = storeToRefs(loadingStore)
const apptitle = import.meta.env.VITE_APP_TITLE
</script>

<style scoped>
.app-loading {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
    background-size: 200% 200%;
    animation: gradientShift 3s ease infinite;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    overflow: hidden;
}

.app-loading::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background:
        radial-gradient(circle at 20% 50%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
        radial-gradient(circle at 80% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
        radial-gradient(circle at 40% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
    animation: float 6s ease-in-out infinite;
}

.loading-container {
    position: relative;
    z-index: 1;
    text-align: center;
    animation: fadeInUp 0.8s ease-out;
}

.loading-spinner {
    width: 80px;
    height: 80px;
    position: relative;
    margin: 0 auto 30px;
}

.loading-spinner::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    border-top: 3px solid #ffffff;
    animation: spin 1.2s cubic-bezier(0.68, -0.55, 0.265, 1.55) infinite;
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.3);
}

.loading-spinner::after {
    content: '';
    position: absolute;
    top: 8px;
    left: 8px;
    width: calc(100% - 16px);
    height: calc(100% - 16px);
    border: 2px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    border-right: 2px solid rgba(255, 255, 255, 0.6);
    animation: spin 0.8s linear infinite reverse;
}

.loading-text {
    color: #ffffff;
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 15px;
    opacity: 0.95;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    letter-spacing: 1px;
    animation: pulse 2s ease-in-out infinite;
}

.loading-subtitle {
    color: rgba(255, 255, 255, 0.8);
    font-size: 16px;
    font-weight: 400;
    text-shadow: 0 1px 5px rgba(0, 0, 0, 0.2);
    letter-spacing: 0.5px;
}

.loading-dots {
    display: inline-block;
    animation: dots 1.8s infinite;
    font-weight: 500;
}

.loading-dots::after {
    content: '';
    animation: dots 1.8s infinite;
}

.loading-progress {
    width: 200px;
    height: 4px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 2px;
    margin-top: 25px;
    overflow: hidden;
    position: relative;
}

.loading-progress::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.8), transparent);
    animation: progress 2s ease-in-out infinite;
}

@keyframes gradientShift {
    0% {
        background-position: 0% 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0% 50%;
    }
}

@keyframes float {
    0%,
    100% {
        transform: translateY(0px) rotate(0deg);
    }

    50% {
        transform: translateY(-20px) rotate(180deg);
    }
}

@keyframes fadeInUp {
    0% {
        opacity: 0;
        transform: translateY(30px);
    }

    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

@keyframes pulse {
    0%,
    100% {
        opacity: 0.95;
        transform: scale(1);
    }

    50% {
        opacity: 1;
        transform: scale(1.05);
    }
}

@keyframes dots {
    0%,
    20% {
        content: '';
    }

    40% {
        content: '.';
    }

    60% {
        content: '..';
    }

    80%,
    100% {
        content: '...';
    }
}

@keyframes progress {
    0% {
        left: -100%;
    }

    100% {
        left: 100%;
    }
}

/* 过渡动画 */
.loading-fade-enter-active,
.loading-fade-leave-active {
    transition: all 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.loading-fade-enter-from {
    opacity: 0;
    transform: scale(0.95);
}

.loading-fade-leave-to {
    opacity: 0;
    transform: scale(0.95);
    visibility: hidden;
    pointer-events: none; /* 防止遮挡下层点击 */
}

/* 响应式设计 */
@media (max-width: 768px) {
    .loading-text {
        font-size: 24px;
    }

    .loading-subtitle {
        font-size: 14px;
    }

    .loading-spinner {
        width: 60px;
        height: 60px;
    }

    .loading-progress {
        width: 150px;
    }
}

@media (max-width: 480px) {
    .loading-text {
        font-size: 20px;
    }

    .loading-subtitle {
        font-size: 13px;
    }

    .loading-spinner {
        width: 50px;
        height: 50px;
    }

    .loading-progress {
        width: 120px;
    }
}
</style>
